package xml.model;

import java.util.ArrayList;
import java.util.List;
import org.jdom.Element;

/**
 * Classe représentant un contact. Elle peut générer elle même le flux xml la représentant
 * @author marya
 */
public class Contact extends xml.jdom.AbstractJDOMElement {

    private Integer idContact;
    private String nom;
    private String uri;
    private String prenom;
    private List<Email> emails;
    private List<Telephone> tels;
    private Adresse adresse;

    public Contact(Integer idEtudiant) {
        this.idContact = idEtudiant;
        this.emails = new ArrayList<Email>();
        this.tels = new ArrayList<Telephone>();
    }

    public Contact(String nom, String prenom, Adresse adresse) {
        this.nom = nom;
        this.prenom = prenom;
        this.adresse = adresse;
        this.emails = new ArrayList<Email>();
        this.tels = new ArrayList<Telephone>();
    }

    public Integer getIdContact() {
        return idContact;
    }

    public void setIdContact(Integer idContact) {
        this.idContact = idContact;
    }

    public Adresse getAdresse() {
        return adresse;
    }

    public void setAdresse(Adresse adresse) {
        this.adresse = adresse;
    }

    public List<Email> getEmails() {
        return emails;
    }

    public void setEmails(List<Email> emails) {
        this.emails = emails;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public List<Telephone> getTels() {
        return tels;
    }

    public void setTels(List<Telephone> tels) {
        this.tels = tels;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    @Override
    public String toString() {
        return "promotion.model.Etudiant[" + this.idContact + ":" + this.nom + "]";
    }

    public Element toJDOMElement() {
        Element racine = new Element("contact");

        if (uri != null) {
            racine.setAttribute("href", uri);
        }

        if (nom != null) {
            Element eNom = new Element("nom");
            eNom.setText(nom);
            racine.addContent(eNom);
        }

        if (prenom != null) {
            Element ePrenom = new Element("prenom");
            ePrenom.setText(prenom);
            racine.addContent(ePrenom);
        }


        Element eTels = new Element("tels");
        if (tels != null && !tels.isEmpty()) {
            for (Telephone tel : tels) {
                eTels.addContent(tel.toJDOMElement());
            }
        }
        racine.addContent(eTels);


        Element eEmails = new Element("emails");
        if (emails != null && !emails.isEmpty()) {
            for (Email email : emails) {
                eEmails.addContent(email.toJDOMElement());
            }
        }
        racine.addContent(eEmails);

        if (adresse != null) {
            racine.addContent(adresse.toJDOMElement());
        }

        return racine;
    }

    public boolean hasTel(Type type) {
        boolean result = false;
        for (Telephone tel : getTels()) {
            if (tel.getType().equals(type)) {
                result = true;
                break;
            }
        }
        return result;
    }

    public boolean hasEmail(Type type) {
        boolean result = false;
        for (Email email : getEmails()) {
            if (email.getType().equals(type)) {
                result = true;
                break;
            }
        }
        return result;
    }
}
